#------------------------------------------------------------------------------
# Project:     Multi-Programmer Game Demo
# Name:        Ball.py
# Purpose:     Bouncing Ball Object
#
# Authors:     Chris Spooner, Chris La-Roo
# Main Author: Chris Spooner
#
# Created:     11/30/2012
# Copyright:   (c) Chris Spooner 2012
# Licence:     New BSD
#------------------------------------------------------------------------------
#took most from pong
import pygame, math, random, time, Counter
from Counter import Counter
class Ball():
    def __init__(self, images, speed, screenSize, waitMax = 1000):
        self.surfaces = []
        for image in images:
            self.surfaces += [pygame.image.load(image)]
        self.frame = 0
        self.waitCount = 0
        self.waitMax = waitMax
        self.maxFrame = len(self.surfaces)-1
        self.surface = self.surfaces[self.frame]
        self.rect = self.surface.get_rect()
        self.radius = self.rect.width/2
        self.speed = speed
        self.screenWidth = screenSize[0]
        self.screenHeight = screenSize[1]
    
    def move(self):
        self.rect = self.rect.move(self.speed)
        
        if self.waitCount < self.waitMax:
            self.waitCount += 1
        else:
            self.waitCount = 0
        
            if self.frame < self.maxFrame:
                self.frame += 1
            else: 
                self.frame = 0
            self.surface = self.surfaces[self.frame]
#   took from ion driver 
    def animate(self):
        if self.hit == True:
            if self.waitCount < self.waitMax:
                self.waitCount += 1
            else:
                self.waitCount = 0
                if self.frame < self.maxFrame:
                    self.frame += 1
                else:
                    self.frame = 0
                    self.hit = False
                    self.ran = True 
                    self.living = False

                self.surface = self.surfaces[self.frame]  
            
    def place(self, pt):
        self.rect = self.rect.move(pt)
        
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))

    
    def wallCollide(self):
        if (self.rect.left < 0):
            self.rect.center = (self.screenWidth/2, self.screenHeight/2)
            self.speed[1] = self.speed[1] = 0
            self.speed[0] = self.speed[0] = 5
            time.sleep (1)
            return True
        if (self.rect.top < 0 
            or self.rect.bottom > self.screenHeight):
            self.speed[1] = self.speed[1]*-1
        if (self.rect.right > self.screenWidth):
            self.speed[0] = self.speed[0]*-1
        
            
    def paddleCollide(self, pdl):
        if (self.rect.right > pdl.rect.left+1 
            and self.rect.left < pdl.rect.right+1):
            if (self.rect.bottom > pdl.rect.top+1 and 
                self.rect.top < pdl.rect.bottom+1):
                    self.speed[0] = self.speed[0] * -1
                    self.speed[1] = self.speed[1] * -1
                    tochange = random.randint(0,1)
                    if pdl.shootDir == "":
                        if self.speed[tochange] > 0:
                            self.speed[tochange] = random.randint(4, 5)
                        else:
                            self.speed[tochange] = random.randint(-5, -4)
                        return True
                    elif pdl.shootDir == "up2":
                        self.speed[0] = self.speed[0] * -1
                        self.speed[1] = abs(self.speed[1])*-1
                    elif pdl.shootDir == "down2":
                        self.speed[0] = self.speed[0] * -1
                        self.speed[1] = abs(self.speed[1])
                        

                        
    
                    
                    
                    

